package com.cloud.generator.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.generator.domain.GenTable;
import com.cloud.generator.domain.GenTableColumn;
import com.cloud.generator.service.IGenTableColumnService;
import com.cloud.generator.service.IGenTableService;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "代码生成")
@RestController
@RequestMapping("/generator/tool")
public class GenController extends BaseController {

    @Autowired
    private IGenTableService genTableService;

    @Autowired
    private IGenTableColumnService genTableColumnService;

    @Operation(summary = "查询代码生成列表")
    @RequestPermissions("generator:tool:list")
    @GetMapping("/list")
    public Result genList(GenTable genTable) {
        startPage();
        List<GenTable> list = genTableService.selectGenTableList(genTable);
        return getDataTable(list);
    }

    @Operation(summary = "查询代码生成业务")
    @RequestPermissions("generator:tool:query")
    @GetMapping(value = "/getInfo")
    public Result getInfo(Long tableId) {
        Map<String, Object> map = new HashMap<>();
        map.put("info", genTableService.selectGenTableById(tableId));
        map.put("tables", genTableService.selectAllGenTable());
        map.put("rows", genTableColumnService.selectTableColumnListById(tableId));
        return Result.success("操作成功", map);
    }

    @Operation(summary = "查询数据库列表")
    @RequestPermissions("generator:tool:list")
    @GetMapping("/dataList")
    public Result dataList(GenTable genTable) {
        startPage();
        List<GenTable> list = genTableService.selectDbTableList(genTable);
        return getDataTable(list);
    }

    @Operation(summary = "查询数据表字段列表")
    @RequestPermissions("generator:tool:list")
    @GetMapping(value = "/getColumnList")
    public Result getColumnList(Long tableId) {
        startPage();
        List<GenTableColumn> list = genTableColumnService.selectTableColumnListById(tableId);
        return getDataTable(list);
    }

    @Operation(summary = "导入表结构(保存)")
    @RequestPermissions("generator:tool:import")
    @PostMapping("/importTable")
    public Result importTable(String tables) {
        List<String> tableNames = ToolUtils.setStrToList(tables);
        List<GenTable> tableList = genTableService.selectTableListByNames(tableNames);
        genTableService.importGenTable(tableList);
        return Result.success("操作成功");
    }

    @Operation(summary = "修改代码生成业务")
    @RequestPermissions("generator:tool:edit")
    @PostMapping
    public Result editSave(@RequestBody GenTable genTable) {
        genTableService.checkSubTable(genTable);
        genTableService.updateGenTable(genTable);
        return Result.success("操作成功");
    }

    @Operation(summary = "删除代码生成信息")
    @RequestPermissions("generator:tool:remove")
    @PostMapping("/remove")
    public Result remove(Long[] tableIds) {
        genTableService.deleteGenTableByIds(tableIds);
        return Result.success("操作成功");
    }

    @Operation(summary = "预览代码信息")
    @RequestPermissions("generator:tool:query")
    @GetMapping("/preViewId")
    public Result preViewId(Long tableId) {
        return Result.success("操作成功", genTableService.previewCode(tableId));
    }

    @Operation(summary = "生成代码(下载方式)")
    @GetMapping("/download")
    public void download(String tableName) {
        genTableService.downloadCode(tableName);
    }

    @Operation(summary = "生成代码(自定义路径)")
    @RequestPermissions("generator:tool:batch")
    @GetMapping("/genCode")
    public Result genCode(String tableName) {
        genTableService.setGeneratorCode(tableName);
        return Result.success("操作成功");
    }

    @Operation(summary = "批量生成代码")
    @RequestPermissions("generator:tool:batch")
    @GetMapping("/batchGenCode")
    public void batchGenCode(String tables) {
        List<String> tableNames = ToolUtils.setStrToList(tables);
        genTableService.downloadCode(tableNames);
    }

}